//
//  LibsManager.swift
//  XBasicKit
//
//  Created by x on 2024/8/29.
//

import Foundation
import XLog
import XDesignKit
import IQKeyboardManagerSwift
//import DropDown
import Toast_Swift
import Kingfisher
import KafkaRefresh
import RxSwift
#if DEBUG
//import FLEX
#endif

public final class LibsManager: NSObject {
    /// The default singleton instance.
//    static let shared = LibsManager()
    static let shared: LibsManager = {
            let instance = LibsManager()
            // 这里执行一次性的初始化代码
            return instance
        }()
        
    func setupLibs() {
        let libsManager = LibsManager.shared
        setupLog()
        libsManager.setupTheme()
        libsManager.setupToast()
//        libsManager.setupDropDown()
//        libsManager.setupFLEX()
        libsManager.setupKingfisher()
        libsManager.setupKeyboardManager()
    }
    
    func setupToast() {
        ToastManager.shared.isTapToDismissEnabled = true
        ToastManager.shared.position = .top
        var style = ToastStyle()
        style.backgroundColor = UIColor.Material.red
        style.messageColor = UIColor.Material.white
        style.imageSize = CGSize(width: 20, height: 20)
        ToastManager.shared.style = style
    }
    
    func setupTheme() {
        UIApplication.shared.theme.statusBarStyle = themeService.attribute { $0.statusBarStyle }
    }

//    func setupDropDown() {
//        themeService.typeStream.subscribe(onNext: { (themeType) in
//            let theme = themeType.associatedObject
//            DropDown.appearance().backgroundColor = theme.primary
//            DropDown.appearance().selectionBackgroundColor = theme.primaryDark
//            DropDown.appearance().textColor = theme.text
//            DropDown.appearance().selectedTextColor = theme.text
//            DropDown.appearance().separatorColor = theme.separator
//        }).disposed(by: rx.disposeBag)
//    }
    
    func setupKafkaRefresh() {
        if let defaults = KafkaRefreshDefaults.standard() {
            defaults.headDefaultStyle = .replicatorAllen
            defaults.footDefaultStyle = .replicatorDot
            defaults.theme.themeColor = themeService.attribute { $0.secondary }
        }
    }

    
    func setupKeyboardManager() {
        IQKeyboardManager.shared.enable = true
    }

    func setupKingfisher() {
        // Set maximum disk cache size for default cache. Default value is 0, which means no limit.
        ImageCache.default.diskStorage.config.sizeLimit = UInt(500 * 1024 * 1024) // 500 MB

        // Set longest time duration of the cache being stored in disk. Default value is 1 week
        ImageCache.default.diskStorage.config.expiration = .days(7) // 1 week

        // Set timeout duration for default image downloader. Default value is 15 sec.
        ImageDownloader.default.downloadTimeout = 15.0 // 15 sec
    }

//    func setupFLEX() {
//        #if DEBUG
//        FLEXManager.shared.isNetworkDebuggingEnabled = true
//        #endif
//    }
}

extension LibsManager {

//    func showFlex() {
//        #if DEBUG
//        FLEXManager.shared.showExplorer()
//        analytics.log(.flexOpened)
//        #endif
//    }

    func removeKingfisherCache() -> Observable<Void> {
        return ImageCache.default.rx.clearCache()
    }

    func kingfisherCacheSize() -> Observable<Int> {
        return ImageCache.default.rx.retrieveCacheSize()
    }
}
